#include "StdAfx.h"
#include "SyntacticAnalyzer.h"

CSyntacticAnalyzer::CSyntacticAnalyzer(void)
{
}

vector <treePtr> CSyntacticAnalyzer::GetTrees(vector <string> tokens)
{
	vector<string>::iterator it;
	vector <treePtr> trees;

	if ( tokens.empty() )
	{
		return trees;
	}

	it = tokens.begin();

	while ( it != tokens.end() )
	{
		if ( ExpectationCommand(it, trees) )
		{	
			++it;
			if ( it != tokens.end() )
			{
				ExpectationSemicolon(it);
			}
			else
			{
				cout << "Error! ; expected." << endl;
			}
		}
		else
		{
			ExpectationWhile(it, trees, tokens);
		}
	}

	return trees;
}

bool CSyntacticAnalyzer::ExpectationSemicolon(vector <string>::iterator & tokens)
{
	string token = *tokens;
	if ( token == ";" )
	{
		++tokens;

		return true;
	}

	++tokens;

	cout << "Error! ; expected." << endl;

	return false;
}

bool CSyntacticAnalyzer::ExpectationLeftBracket(vector <string>::iterator & tokens)
{
	string token = *tokens;
	if ( token == "(" )
	{
		++tokens;

		return true;
	}

	cout << "Error! ( expected." << endl;

	return false;
}

bool CSyntacticAnalyzer::ExpectationRightBracket(vector <string>::iterator & tokens)
{
	string token = *tokens;
	if ( token == ")" )
	{
		++tokens;

		return true;
	}

	cout << "Error! ) expected." << endl;

	return false;
}

bool CSyntacticAnalyzer::ExpectationVariable(vector <string>::iterator & tokens, treePtr & trees)
{
	

	return false;
}

bool CSyntacticAnalyzer::ExpectationCommand(vector <string>::iterator & tokens, vector <treePtr> & trees)
{
	string token = *tokens;
	treePtr treeNode = new tree;
	treeNode->type = VARIABLE;

	if ( token ==  "right" )
	{
		treeNode->type = RIGHT;
	}
	else if ( token ==  "quack" )
	{ 
		treeNode->type = QUACK;
	}
	else if ( token == "fly" )
	{
		treeNode->type = FLY;
	}
	else if ( token == "exit" )
	{
		treeNode->type = EXIT;
	}
	
	if ( treeNode->type != VARIABLE )
	{
		trees.push_back(treeNode);

		return true;
	}

	return false;
}

bool CSyntacticAnalyzer::ExpectationWhile(vector <string>::iterator & tokens, vector <treePtr> & trees, vector <string> & tokensArr)
{
	string token = *tokens;

	treePtr treeNode = new tree;

	if ( token ==  "while" )
	{
		treeNode->type = WHILE;
		++tokens;

		if ( tokens == tokensArr.end() || !ExpectationLeftBracket(tokens) )
		{
			cout << "Error! Variable expected." << endl;
			return false;
		}

		if ( tokens == tokensArr.end() )
		{
			cout << "Error! Variable expected." << endl;
			return false;
		}

		++tokens;

		if ( tokens == tokensArr.end() || !ExpectationRightBracket(tokens) )
		{
			cout << "Error! Variable expected." << endl;
			return false;
		}

		if ( tokens == tokensArr.end() )
		{
			cout << "Error! Command or while expected." << endl;
			return false;
		}
		ExpectationCommand(tokens, treeNode->right) || ExpectationWhile(tokens, treeNode->right, tokensArr);
		trees.push_back(treeNode);

		return true;
	}

	++tokens;

	cout << "Error! Command or while expected." << endl;

	return false;
}

CSyntacticAnalyzer::~CSyntacticAnalyzer(void)
{
}